package com.chalk.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.chalk.dao.NewsTypeDao;
import com.chalk.exception.ColumnNotEmptyException;
import com.chalk.model.NewsType;
import com.chalk.service.NewsTypeService;
import com.chalk.util.MpUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author glq
 * @since 2020-12-19
 */

@Service
public class NewsTypeServiceImpl extends ServiceImpl<NewsTypeDao, NewsType> implements NewsTypeService {

    @Autowired
    MpUtil<NewsType> mpUtil;
    @Override
    public int addNewsType(NewsType entity) {
        return baseMapper.insert(entity);
    }





    @Override
    public Page<NewsType> getNewsTypeForPage(Map<String, Object> map) throws ColumnNotEmptyException {
        EntityWrapper<NewsType> wrapper=mpUtil.getWrapper();
        Page<NewsType> newsTypePage=mpUtil.getPage(map);
        newsTypePage.setDescs(mpUtil.getDesc("create_time"));
        return selectPage(newsTypePage,wrapper);
    }

    @Override
    public Boolean saveNewsType(NewsType newsType) {
        newsType.setCreateTime(LocalDateTime.now());
        return insert(newsType);
    }


    @Override
    public Boolean updateNewsType(NewsType newsType) {
        newsType.setUpdateTime(LocalDateTime.now());

        return updateById(newsType);
    }

    @Override
    public Boolean deleteNewsType(Integer newsTypeId) {
        NewsType newsType=new NewsType();
        newsType.setIsDelete(1);
        newsType.setNewsTypeId(newsTypeId);
        return updateById(newsType);
    }

    @Override
    public List<NewsType> newsTypeToList() {
        EntityWrapper<NewsType> wrapper=mpUtil.getWrapper();
        return selectList(wrapper);
    }
}
